<?php


namespace App\Admin\Controllers;


use App\Admin\Actions\ProductBatch\AddArea;
use App\Models\ProductsBatchModel;
use App\Models\ReportModel;
use App\Models\WarehouseModel;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Form;
use Encore\Admin\Widgets\Table;

class WarehouseController extends AdminController
{
    use HasResourceActions;

    protected $title = '我的仓库';

    public function index(Content $content)
    {
        return $content
            ->header('仓库管理')
            ->description(trans('admin.list'))
            ->body($this->grid()->render());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new WarehouseModel());

        $grid->column('warehouse_id', '仓库ID');
        $grid->column('warehouse_name', '仓库名称')->editable();
        $grid->column('remark', '备注')->editable();
        $grid->column('areas', '可用区域')->display(function () {
            return $this->WarehouseArea()->count();
        })->modal('区域', function ($model) {
            $loss = $model->WarehouseArea()->take(100)->get()->map(function ($loss) {
                return $loss->only(['warehouse_area_id','area_name', 'remark']);
            });
            return new Table(['ID', '区域名', '备注'], $loss->toArray());
        });
        $grid->column('created_at', '添加时间');

        $grid->actions(function ($action) {
            $action->disableView();
            $action->add(new AddArea());
        });
        return $grid;
    }


    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new WarehouseModel());
        //        $form->hidden('product_name', '产品名称');
        $form->text('warehouse_name', '仓库名')->required();
        $form->text('remark', '备注');


        $form->disableCreatingCheck();
        $form->disableEditingCheck();
        $form->disableViewCheck();
        $form->display('created_at', 'Created At');
        $form->display('updated_at', 'Updated At');
        return $form;
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('仓库管理')
            ->description(trans('admin.create'))
            ->body($this->form());
    }

    /**
     * Edit interface.
     *
     * @param $id
     *
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('仓库管理')
            ->description(trans('admin.edit'))
            ->body($this->form()->edit($id));
    }
}
